package hd.util.http;

import org.apache.http.Header;
import org.apache.http.cookie.Cookie;

import java.util.List;

/**
 * Http请求头
 * <p/>
 * Created by Haidy on 15/2/3.
 */
public class HttpResponse {

    // 响应头
    public List<Header> headers;
    // 响应代码
    public int responseCode;
    // 信息
    public String info;
    // Cookies
    public List<Cookie> cookies;
    // text
    public String text;


    /**
     * 是否有请求头
     *
     * @param name name
     * @return boolean
     */
    public boolean hasHeader(String name) {
        if (headers == null) {
            return false;
        }
        for (Header header : headers) {
            if (header.getName().equals(name)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 是否含有Cookie
     *
     * @return cookie
     */
    public boolean hasCookie() {
        return cookies != null && cookies.size() > 0;
    }

    /**
     * 获取字符串的完整Cookies
     *
     * @return String
     */
    public String getCookie() {
        if (cookies == null) {
            return null;
        }
        String cookie = "";
        for (Cookie ck : cookies)
            cookie += (";" + ck.getName() + "=" + ck.getValue());
        if (cookie.length() > 0)
            cookie = cookie.substring(1);
        return cookie;
    }

}
